package com.ken.apollo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 页面控制器
 * 处理静态页面路由
 */
@Controller
public class PageController {

    /**
     * 首页
     */
    @GetMapping("/")
    public String index(Model model) {
        return "index";
    }

    /**
     * 商品列表页面
     */
    @GetMapping("/products")
    public String products(Model model,
                          @RequestParam(value = "category", required = false) String category,
                          @RequestParam(value = "sort", required = false) String sort) {

        // 添加筛选和排序参数到模型
        model.addAttribute("selectedCategory", category);
        model.addAttribute("selectedSort", sort);

        return "products";
    }

    /**
     * 商品详情页面
     */
    @GetMapping("/product/{id}")
    public String productDetail(@PathVariable("id") Long id, Model model) {
        // 传递产品ID到前端，由JavaScript调用API获取详细数据
        model.addAttribute("productId", id);
        return "product-detail";
    }

    /**
     * 联系我们页面
     */
    @GetMapping("/contact")
    public String contact(Model model) {
        return "contact";
    }

    /**
     * 处理联系表单提交（重定向到API）
     * 注意：实际的表单提交应该使用 /api/contact/submit API接口
     * 这个方法保留用于向后兼容
     */
    @PostMapping("/contact/submit")
    public String submitContact(
            @RequestParam("firstName") String firstName,
            @RequestParam("lastName") String lastName,
            @RequestParam("email") String email,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "company", required = false) String company,
            @RequestParam("subject") String subject,
            @RequestParam("message") String message,
            @RequestParam(value = "newsletter", required = false) String newsletter,
            RedirectAttributes redirectAttributes) {

        // 提示用户使用新的API接口
        redirectAttributes.addFlashAttribute("infoMessage",
            "Please use the contact form on the page to submit your inquiry. The form will be processed automatically.");

        return "redirect:/contact";
    }

    /**
     * 搜索产品
     */
    @GetMapping("/search")
    public String searchProducts(@RequestParam("q") String query, Model model) {
        model.addAttribute("searchQuery", query);
        return "products";
    }

    /**
     * 产品分类页面
     */
    @GetMapping("/category/{categoryName}")
    public String categoryProducts(@PathVariable("categoryName") String categoryName, Model model) {
        model.addAttribute("selectedCategory", categoryName);
        return "products";
    }

    /**
     * 后台联系信息管理页面
     */
    @GetMapping("/backs/contact_manager")
    public String contactManager() {
        return "backs/contact_manager";
    }

    /**
     * 邮件测试页面
     */
    @GetMapping("/mail-test")
    public String mailTest() {
        return "mail-test";
    }

}
